package com.mcptt.db.op;

import java.util.ArrayList;
import java.util.List;

import com.cloudptt.api.product.log.Log;
import com.mcptt.db.DBProvider;
import com.mcptt.db.DbConst;
import com.mcptt.db.model.GroupInfo;
import com.mcptt.db.model.GroupMembInfo;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class GroupMembsDbOperator {

	public static String THIS_FILE = "DbOperator";
	private Context ctx = null;


	public GroupMembsDbOperator(Context ctx)
	{
		this.ctx = ctx;
	}


	public void  resetGrpMemCallState()
	{
		ContentResolver cr = ctx.getContentResolver();

		ContentValues cv = new ContentValues();	    	    
		cv.put(DbConst.COL_MEMBER_STATUS, "0");	
		cr.update(DbConst.MEMBER_URI, cv,null, null);
	}

	public  void changeCallState2Nomal() {

		ContentResolver cr = ctx.getContentResolver();

		ContentValues cv = new ContentValues();		
		String selection = DbConst.COL_MEMBER_STATUS +"= 10 ";
		cv.put(DbConst.COL_MEMBER_STATUS, "1");	
		cr.update(DbConst.MEMBER_URI, cv,selection, null);
	}

	public boolean isGrpHasMem(String uid)
	{
		ContentResolver cr = ctx.getContentResolver();

		boolean hasMem = false;
		String selection = DbConst.COL_MEMBER_GROUP_NUM +"=?";

		Cursor cursor = null;
		try 
		{
			cursor = cr.query(DbConst.MEMBER_URI, DBProvider.MEMBER_FULL_PROJECTION, selection, new String[]{uid},null);

			if(cursor.getCount()>0)
			{
				hasMem = true;
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		finally
		{
			if(cursor != null)
			{
				cursor.close();
				cursor = null;
			}
		}

		return hasMem;
	}

	public List<GroupMembInfo> getAllGroupMembInfo(GroupInfo userInfo)
	{
		ContentResolver cr = ctx.getContentResolver();
		String selection = DbConst.COL_MEMBER_GROUP_NUM +"=?";

		List<GroupMembInfo> groupMemblist = new ArrayList<GroupMembInfo>();  
		Cursor cursor = null;
		try 
		{
			cursor = cr.query(DbConst.MEMBER_URI, DBProvider.MEMBER_FULL_PROJECTION, selection, new String[]{userInfo.getMcId()},null);

			for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext())
			{
				GroupMembInfo info = new GroupMembInfo(cursor);
				groupMemblist.add(info);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally
		{
			if(cursor != null)
			{
				cursor.close();
				cursor = null;
			}
		}

		return groupMemblist;
	}

	public void addMemb(GroupMembInfo info)
	{
		ContentResolver cr = ctx.getContentResolver();
		String selection = DbConst.COL_MEMBER_GROUP_NUM +"=? and "+ DbConst.COL_MEMBER_NUM +"=?";

		Cursor cursor = null;
		try 
		{
			cursor =  cr.query(DbConst.MEMBER_URI, DBProvider.MEMBER_FULL_PROJECTION, selection, new String[]{info.getGroupNum(),info.getNum()}, null);
			if (cursor == null
					|| cursor.getCount() <= 0)
			{
				cr.insert(DbConst.MEMBER_URI, info.getContentValues());
				Log.debug(this.getClass(), "addMemb end");
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally
		{
			if(cursor != null)
			{
				cursor.close();
				cursor = null;
			}
		}
	}
	public void deleteAllMembForGroupIp(GroupMembInfo info)
	{
		ContentResolver cr = ctx.getContentResolver();
		String selection = DbConst.COL_MEMBER_GROUP_NUM +"=?";
		int resutl = cr.delete(DbConst.MEMBER_URI, selection, new String[]{info.getGroupNum()} );
		Log.debug(this.getClass(), "deleteAllMembForGroupIp result = "+ resutl);
	}
	public void deleteMemb(GroupMembInfo info)
	{
		ContentResolver cr = ctx.getContentResolver();
		String selection = DbConst.COL_MEMBER_GROUP_NUM +"=? and "+ DbConst.COL_MEMBER_NUM +"=?";
		int resutl = cr.delete(DbConst.MEMBER_URI, selection, new String[]{info.getGroupNum(),info.getNum()} );
		Log.debug(this.getClass(), "deleteMemb result = "+ resutl);
	}
	public void updateMemb(GroupMembInfo info)
	{
		ContentResolver cr = ctx.getContentResolver();
		String selection = DbConst.COL_MEMBER_GROUP_NUM +"=? and "+ DbConst.COL_MEMBER_NUM +"=?";
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_MEMBER_NAME, info.getName());	
		cv.put(DbConst.COL_MEMBER_NUM, info.getNum());	

		int ret = cr.update(DbConst.MEMBER_URI, cv,selection, new String[]{info.getGroupNum(),info.getNum()});
		if(ret <= 0)
		{
			addMemb(info);
		}
	}

	public void updateMembStatus(GroupMembInfo info)
	{
		String selection = DbConst.COL_MEMBER_NUM +"=? or "+DbConst.COL_MEMBER_NUM+"=? and "
				+ DbConst.COL_MEMBER_GROUP_NUM +"=? and "+DbConst.COL_MEMBER_STATUS+"!=? ";

		//只接�?0�?1�?10数据状�?�，其他都为0
		if(info.getStatus().equals("2")
				||info.getStatus().equals("4")||info.getStatus().equals("5"))
		{
			return;
		}

		ContentResolver cr = ctx.getContentResolver();
		ContentValues cv = new ContentValues();
		if(info.getStatus().equals("3"))
		{
			info.setStatus("0");
		}
		cv.put(DbConst.COL_MEMBER_STATUS, info.getStatus());	
		String num = info.getNum();
//		String uid =info.getUserId();

		cr.update(DbConst.MEMBER_URI, cv,selection, new String[]{num,info.getGroupNum(),"10"});
	}
}
